package com.dcbeida.activity.sign;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.common.utils.DateUtil;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.bean.SignInfo;
import com.dcbeida.utils.CompressImageUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.OnClick;

public class SignQrCodeActivity extends BaseActivity  implements SignContract.View{

    @Bind(R.id.img_back)
    ImageView img_back;

    @Bind(R.id.iv_qr_code)
    ImageView iv_qr_code;
    @Bind(R.id.tv_refuse)
    TextView tvRefuse;
    @Bind(R.id.tv_exist)
    TextView tvExist;
    @Bind(R.id.tv_time)
    TextView tv_time;



    //类型
    private int type = 0; // 如果为2 是两个按钮的

    private int patientId;

    private SignPresenter signPresenter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_sign_qrcode;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParams();
    }

    private void initParams() {
        signPresenter =new SignPresenter(this,this);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            patientId = bundle.getInt("patientId");
            type = bundle.getInt("type",0);
        }


        if(type == 2){
            tvRefuse.setVisibility(View.GONE);
            tvExist.setText("拒签或者不签署,返回");
        }else if(type == 0){
            tvExist.setText("暂不签署,返回");
        }else if(type == 1){
            tvExist.setText("暂不签署,继续提交订单");
        }

        signPresenter.getSignQrCode(patientId);
        long time = 30*60*1000;
        Date afterDate = new Date(new Date().getTime() + time);//30分钟后的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tv_time.setText("二维码在 "+sdf.format(afterDate)+" 前有效");
    }



    @OnClick({R.id.img_back,R.id.tv_confirm,R.id.tv_exist,R.id.tv_refuse})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                setResult(RESULT_OK,new Intent());
                finish();
                break;
            case R.id.tv_confirm:
                signPresenter.queryPatient(patientId);
                break;
            case R.id.tv_refuse:
                signPresenter.rejectSign(patientId);
                break;
            case  R.id.tv_exist:
                if(type  == 2) {
                    finish();
                }else if(type == 0){
                    Intent intent = new Intent();
                    intent.putExtra("status",99);
                    setResult(RESULT_OK,intent);
                    finish();
                }else if(type == 1){
                    Intent intent = new Intent();
                    intent.putExtra("status",99);
                    setResult(RESULT_OK,intent);
                    finish();
                }
                break;


        }
    }

    @Override
    public void showTip(String msg) {
        super.showTip(msg);
    }

    @Override
    public void showQrCode(SignInfo signInfo) {
        if(!TextUtils.isEmpty(signInfo.getQrcode())){
            Bitmap bitmap = CompressImageUtils.stringToBitmap(signInfo.getQrcode());
            iv_qr_code.setImageBitmap(bitmap);
        }
    }

    @Override
    public void showSignSuccess() {
        showTip("该患者已成功签署");
        Intent intent = new Intent();
        intent.putExtra("status",1);
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    public void showSignStatus(int status) {
        Intent intent = new Intent();
        intent.putExtra("status",status);
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    public void setPresenter(SignContract.Present presenter) {

    }
}
